/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.AccessorClassTypeInfo;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;

public class AccessorInitializeChildCollectionGen extends ClassMethodGenerator
{
	private MediateType entityInterfaceType;
	private IGspCommonDataType dataType;
	private java.util.Map<String, String> childAndFieldNames;
	private AccessorClassTypeInfo accessorClassTypeInfo;
	public AccessorInitializeChildCollectionGen(MediateType entityInterfaceType, IGspCommonDataType dataType, java.util.Map<String, String> childAndFieldNames, AccessorClassTypeInfo accessorClassTypeInfo)
	{
		this.entityInterfaceType = entityInterfaceType;
		this.dataType = dataType;
		this.childAndFieldNames = childAndFieldNames;
		this.accessorClassTypeInfo = accessorClassTypeInfo;
	}

	@Override
	protected Block buildMethodBody() {
		Block block=ast.newBlock();
		addIfNullStatment(block);
		addChildsStatements(block);
		return block;
	}

	private void addIfNullStatment(Block block) {
		IfStatement ifStatement=ast.newIfStatement();
		addIfExpression(ifStatement);
		addifThen(ifStatement);
		block.statements().add(ifStatement);
	}

	private void addIfExpression(IfStatement ifStatement) {
		InfixExpression infixExpression=ast.newInfixExpression();
		infixExpression.setLeftOperand(GeneratorUtil.getgetInnerDataStatemnet(ast));
		infixExpression.setOperator(InfixExpression.Operator.EQUALS);
		infixExpression.setRightOperand(ast.newNullLiteral());

		ifStatement.setExpression(infixExpression);
	}


	private void addifThen(IfStatement ifStatement) {
		ifStatement.setThenStatement(ast.newReturnStatement());
	}

	private void addChildsStatements(Block block)
	{
		if(dataType instanceof IGspCommonObject ==false)
			return;
		List<String> objects = new ArrayList<String>();
		for (IGspCommonObject childObject :((IGspCommonObject)dataType).getContainChildObjects())
		{
			if(!objects.contains(childObject.getCode()))
			{
				objects.add(childObject.getCode());
			}
			else {
				continue;
			}
			addChildInitializers(childObject,block);
		}
	}

	private void addChildInitializers(IGspCommonObject childObject, Block block) {
		Assignment assignment=ast.newAssignment();
		assignment.setLeftHandSide(ast.newSimpleName(getChildFieldName(childObject)));
		assignment.setOperator(Assignment.Operator.ASSIGN);

		MethodInvocation instanceCollectionInvocation = ast.newMethodInvocation();
		instanceCollectionInvocation.setName(ast.newSimpleName(NewChildCollectionGenerator.getMethodName(childObject)));
		instanceCollectionInvocation.arguments().add(ast.newThisExpression());

		assignment.setRightHandSide(instanceCollectionInvocation);

		ExpressionStatement expressionStatement=ast.newExpressionStatement(assignment);
		block.statements().add(expressionStatement);
	}


	private String getChildFieldName(IGspCommonObject childObject) {
		return childAndFieldNames.get(childObject.getCode());
	}

	private String getChildObjCollectionTypeName(IGspCommonObject childObject) {
		return accessorClassTypeInfo.getChildObjAccessorCollectionTypes().get(childObject.getCode()).getFullName();
	}

	public static final String MethodName = "initializeChildCollection";

	@Override
	protected String getMethodName(){return MethodName;}
	@Override
	protected TypeInfo getReturnType() {
		return new TypeInfo(void.class.getTypeName());
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		ArrayList<Modifier.ModifierKeyword> list =new ArrayList<>();
		list.add(Modifier.ModifierKeyword.PRIVATE_KEYWORD);
		return list;
	}

	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		return null;
	}
}
